package com.itheima.service.smsService.impl;

import cn.hutool.core.convert.Convert;
import com.itheima.commons.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.sms.SmsHomeAdvertise;
import com.itheima.mapper.smsMapper.SmsHomeAdvertiseMapper;
import com.itheima.service.smsService.SmsHomeAdvertiseService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@DubboService
public class SmsHomeAdvertiseServiceImpl implements SmsHomeAdvertiseService {

    @Autowired
    SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    //添加广告
    @Override
    public void save(SmsHomeAdvertise advertise) {
        smsHomeAdvertiseMapper.insert(advertise);
    }

    @Override
    public void delete(List<Long> ids) {
        smsHomeAdvertiseMapper.deleteBatchIds(ids);
    }

    @Override
    public PageResult<SmsHomeAdvertise> findByPage(String endTime,String name,Integer pageNum,Integer pageSize,Integer type) {
        LocalDateTime localEndTime=null;
        if (endTime!=null) {
            localEndTime=Convert.toLocalDateTime(endTime);
        }

        IPage<SmsHomeAdvertise> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeAdvertise> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type!=null,SmsHomeAdvertise::getType,type);
        queryWrapper.eq(localEndTime!=null,SmsHomeAdvertise::getEndTime,localEndTime);
        queryWrapper.like(name!=null,SmsHomeAdvertise::getName,name);
        smsHomeAdvertiseMapper.selectPage(page,queryWrapper);
        PageResult<SmsHomeAdvertise> pageBean = new PageResult<>(page);

        return pageBean;
    }


    @Override
    public void update(Long id, Integer status) {
        smsHomeAdvertiseMapper.updateStatus(id,status);
    }

    @Override
    public void updataById(Long id, SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertise.setId(id);
        smsHomeAdvertiseMapper.updateById(smsHomeAdvertise);
    }

    @Override
    public SmsHomeAdvertise getAdvertiseById(Long id) {
        return smsHomeAdvertiseMapper.selectById(id);
    }
}